package com.zhongyu.controller;

import com.zhongyu.pojo.Cangku;
import com.zhongyu.pojo.User;
import com.zhongyu.service.CangKuService;
import com.zhongyu.vo.CangKuVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
/*
仓库
 */
@RestController
@RequestMapping("/jibenxinxi/ck")
public class CangKuController {
    @Resource
    private CangKuService cangKuService;
    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CangKuVo cangKuVo){
        PageResult pageResult=cangKuService.searchAll(pageNo,pageSize,cangKuVo);
        return ResultVo.success(pageResult);

    }
    @GetMapping("/list")
    public ResultVo list() {
        List<Cangku> list = cangKuService.searchAll();
        return ResultVo.success(list);
    }
    @GetMapping("/list1")
    public ResultVo list1(HttpSession session) {
        User loginUser= (User) session.getAttribute("loginUser");
        List<Cangku> list = cangKuService.searchByuserId(loginUser.getId());
        return ResultVo.success(list);
    }
    @PostMapping("/update")
    public ResultVo update(@RequestBody Cangku cangku, HttpSession session) {
        try {
            cangKuService.update(cangku);
            return ResultVo.success("修改仓库成功!");
        }catch (Exception ex){

            return ResultVo.failure("修改仓库失败!",ex);
        }
    }

    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            cangKuService.del(id);
            return ResultVo.success("删除仓库成功!");
        } catch (Exception ex) {

            return ResultVo.failure("删除仓库失败！", ex);
        }
    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody Cangku cangku, HttpSession session){
        try {
            cangKuService.add(cangku);
            return ResultVo.success("添加仓库成功!");
        }catch (Exception ex){
            return ResultVo.failure("添加仓库失败!",ex);
        }
    }
}
